package NM;

import java.util.Stack;

public class MinStack {
    //定义两个成员变量；
    public Stack<Integer> stack;
    public Stack<Integer> minStack;
    public MinStack(){
        stack=new Stack<>();
        minStack=new Stack<>();
    }
    //入栈操作；
    public void push(int val){
        stack.push(val);
        if(minStack.empty()){
            minStack.push(val);
        }else{
            int peek=minStack.peek();
            if(val<=peek){
                minStack.push(val);
            }
        }
    }
    //出栈操作；
    public void pop(){
        if(stack.empty()){
            return;
        }
        int popVal=stack.pop();
        if(popVal== minStack.pop()){
            minStack.pop();
        }
    }
    //获取栈顶元素，但不删除栈顶元素；
    public int top(){
        if(stack.empty()){
            return -1;
        }
        return stack.peek();
    }
    //获取栈里最小元素；
    public int getMin(){
        if(minStack.empty()){
            return -1;
        }
        return minStack.peek();
    }
}
